package com.example.CarFee;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.trainfee.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class PinlunActivity extends Activity {
	private ListView listView;
	private ImageView backBtn;
	private Button pinglunBtn;
	private EditText content;
	private SimpleAdapter adapter;
	private List<Map<String, String>> items = new ArrayList<Map<String, String>>();
	private ArrayList<String> contentList;
	private String msg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pinglun);
		listView = (ListView) findViewById(R.id.listView);
		backBtn = (ImageView) findViewById(R.id.backBtn);
		pinglunBtn = (Button) findViewById(R.id.pinglunBtn);
		content = (EditText) findViewById(R.id.content);
		Intent intent = getIntent();
		msg = intent.getStringExtra("content");
		System.out.println(msg);
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				String s = "";
				try {
					s = URLEncoder.encode(msg, "UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				contentList = Service.checkReview(s);
				handler.sendEmptyMessage(0x120);
			}
		});
		thread.start();
		pinglunBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!content.getText().toString().equals("")) {
					Thread thread = new Thread(new Runnable() {

						@Override
						public void run() {
							String s = "";
							try {
								s = URLEncoder.encode(content.getText()
										.toString(), "UTF-8");
								msg = URLEncoder.encode(msg, "UTF-8");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							if (Service.Review(msg, s)) {
								handler.sendEmptyMessage(0x121);
							} else {
								handler.sendEmptyMessage(0x122);
							}

						}
					});
					thread.start();
				} else {
					Toast.makeText(PinlunActivity.this, "评论不能为空",
							Toast.LENGTH_SHORT).show();

				}

			}
		});
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0x120) {
				for (int i = 0; i < contentList.size(); i++) {
					HashMap<String, String> item = new HashMap<String, String>();
					item.put("content", contentList.get(i));
					item.put("pinglun", "");
					items.add(item);
				}
				adapter = new SimpleAdapter(PinlunActivity.this, items,
						R.layout.discuss_listview_item, new String[] {
								"content", "pinglun" }, new int[] {
								R.id.content, R.id.pinglun });
				listView.setAdapter(adapter);
			}
			if (msg.what == 0x121) {
				Toast.makeText(PinlunActivity.this, "评论成功", Toast.LENGTH_SHORT)
						.show();
				content.setText("");

			}
			if (msg.what == 0x122) {
				Toast.makeText(PinlunActivity.this, "评论失败", Toast.LENGTH_SHORT)
						.show();
			}
		}
	};
}
